<% add_decidim_page_title(t(".title")) %>
<div class="item_show__header">
  <h1 class="item_show__header-title">
    <%= t ".title" %>
  </h1>
  <div class="flex ml-auto items-center gap-x-4">
    <% if allowed_to?(:update, :newsletter, newsletter: @newsletter) && !@newsletter.sent? %>
      <%= link_to t("actions.edit", scope: "decidim.admin"), [:edit, @newsletter], class: "button button__sm button__secondary" %>
    <% end %>
  </div>
</div>
<div class="item__edit item__edit-1col">
  <div class="item__edit-form">
    <%= decidim_form_for(@form, url: deliver_newsletter_path(@newsletter), method: :post, html: { class: "form form-defaults newsletter_deliver" }, data: { "recipients-count-newsletter-path": recipients_count_newsletter_path(@newsletter) }) do |f| %>
      <%= cell("decidim/announcement", newsletter_attention_callout_announcement, callout_class: "warning") %>
      <%= cell("decidim/announcement", newsletter_recipients_count_callout_announcement, callout_class: "warning") %>
      <div class="form__wrapper">
        <div class="card mb-2">
          <div class="card-section">
            <div class="row column">
              <% if current_user.admin? %>
                <div class="mt-4">
                  <h2 class="card-title"><%= t ".select_users_general" %></h2>
                </div>
                <div class="grid-x mb-4">
                  <div class="cell small-12" id="send_newsletter_to_all_users">
                    <%= label_tag "newsletter_send_to_all_users" do %>
                      <%= radio_button_tag "newsletter[send_to_all_users]", "1", @form.send_to_all_users == true, id: "newsletter_send_to_all_users" %>
                      <%= t(".send_to_all_users") %>
                    <% end %>
                    <span class="help-text"><%= t(".all_users_help", default: "Sends a newsletter to all users.") %></span>
                  </div>
                  <div class="cell small-12" id="send_newsletter_to_verified_users">
                    <%= label_tag "newsletter_send_to_verified_users" do %>
                      <%= radio_button_tag "newsletter[send_to_verified_users]", "1", @form.send_to_verified_users == true, id: "newsletter_send_to_verified_users" %>
                      <%= t(".send_to_verified_users") %>
                    <% end %>
                    <span class="help-text"><%= t(".verified_users_help", default: "Sends a newsletter to all confirmed and verified users via any selected verification method.") %></span>
                  </div>
                  <div id="verification_types_for_select" class="my-4 hidden">
                    <%= cell(
                          "decidim/admin/multi_select_picker",
                          nil,
                          context: {
                            view_context: self,
                            current_user:,
                            select_id: "verification-types-select",
                            field_name: "newsletter[verification_types][]",
                            options_for_select: find_verification_types_for_select(current_organization),
                            selected_values: selected_options(:verification_types).values.flatten,
                            placeholder: t(".select_verification_types"),
                            class: "mt-2 js-verification-multiselect"
                          }
                        ) %>
                  </div>
                </div>
                <hr>
              <% end %>
              <div class="mt-4">
                <h2 class="card-title"><%= t ".select_users_for_participatory_space" %></h2>
              </div>
              <div class="grid-x">
                <div class="cell small-12 medium-6" id="send_newsletter_to_followers">
                  <%= f.check_box :send_to_participants, help_text: t(".participants_help") %>
                </div>

                <div class="cell small-12 medium-6" id="send_newsletter_to_participants">
                  <%= f.check_box :send_to_followers, help_text: t(".followers_help") %>
                </div>

                <div class="cell small-12 medium-6" id="send_newsletter_to_private_members">
                  <%= f.check_box :send_to_private_members, help_text: t(".private_members_help") %>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="card mb-2" id="participatory_spaces_for_select">
          <div class="card-divider">
            <h2 class="card-title"><%= t ".select_spaces" %></h2>
          </div>
          <div class="card-section">
            <div class="row column">
              <%= participatory_spaces_for_select f %>
            </div>
          </div>
        </div>
      </div>
      <div class="item__edit-sticky">
        <div class="item__edit-sticky-container">
          <% unless @newsletter.sent? %>
            <%= f.submit t(".deliver"), id: "deliver-button", class: "button button__sm button__secondary", data: { confirm: t(".confirm_deliver") } %>
            <%= link_to t(".confirm_recipients"), "#",
                        id: "confirm-recipients-link",
                        class: "button button__sm button__secondary hidden",
                        data: { base_url: confirm_recipients_newsletter_path(@newsletter) } %>
          <% end %>
        </div>
      </div>
    <% end %>
  </div>
</div>
